//初始化数据
// addUser("userType", userType);
// addUser("goodspj", goodspj);
var userType = GetUser("userType");
var goodspj = GetUser("goodspj");
//添加类型事件
$("#addbtn").click(function () {
  $("#right-content").load("./html/add_type.html");
});
//评价查询
$("#addbtnpj").click(function () {
  let zxunpeop = $("#zxun-1").val();
  let zxunneir = $("#zxun-2").val();
  // console.log(zxunneir, zxunpeop);
  if (zxunpeop == "" && zxunneir == "") {
    layer.msg("未输入查询内容", { icon: 52, time: 500 });
  } else if (zxunpeop == "" && zxunneir != "") {
    for (let i = 0; i < goodspj.length; i++) {
      var goodspj1 = goodspj.filter(function (item) {
        // console.log(item.id);
        return item.pjgoods == zxunneir;
      });
    }
    Goodspjappend(goodspj1);
    fenye1(goodspj1);
    // console.log(goodspj1);
  } else if (zxunpeop != "" && zxunneir == "") {
    for (let i = 0; i < goodspj.length; i++) {
      var goodspj1 = goodspj.filter(function (item) {
        // console.log(item.id);
        return item.pjuser == zxunpeop;
      });
    }
    Goodspjappend(goodspj1);
    fenye1(goodspj1);
    // console.log(goodspj1);
  } else {
    for (let i = 0; i < goodspj.length; i++) {
      var goodspj1 = goodspj.filter(function (item) {
        // console.log(item.id);
        return item.pjuser == zxunpeop && item.pjgoods == zxunneir;
      });
    }
    Goodspjappend(goodspj1);
    fenye1(goodspj1);
    // console.log(goodspj1);
  }
});
//全选
$("#checkAll").click(function () {
  //获取全选checkbox的状态
  let state = $("#checkAll").prop("checked");
  //将每一项数据前的checkbox改成和全选checkbox一样的状态
  $(".check").prop("checked", state);
});
// 给每一行中的checkbox添加点击事件
$("tbody").on("click", ".check", function () {
  //判断所有的行中，checkbox的状态，是否为全部选中
  // 如果 ，是全选变成true, 否则把全选变成false
  checkAllChecked();
});
//判断全选状态
function checkAllChecked() {
  if ($(".check:not(:checked)").length == 0) {
    //没有选中的个数
    $("#checkAll").prop("checked", true);
  } else {
    $("#checkAll").prop("checked", false);
  }
}

//点击删除的弹出框的点击事件
$("#tbody-1").on("click", "#cc", function () {
  let id = $(this).attr("index-id");
  // 给删除按钮绑定事件即可  用事件委派，这儿是只给一个删除绑定了
  layer.confirm(
    "确认删除？",
    {
      title: "删除",
      btn: ["确认", "取消"], //按钮
    },
    function () {
      //确定的方法
      // console.log(id);
      //赋值给新对象
      let userTypes = userType.filter(function (item) {
        // console.log(item.id);
        return item.ID != id;
      });
      // console.log(id);
      // console.log(userTypes);
      userType = userTypes;
      addUser("userType", userType);
      Typeappend(userType);
      layer.msg("已删除", { icon: 1, time: 500 });
    },
    function () {
      //取消方法
      layer.msg("已取消", { icon: 2, time: 500 });
    }
  );
});

//点击删除的弹出框的点击事件

$("#tbody-2").on("click", "#cc", function () {
  let id = $(this).attr("index-id");
  // 给删除按钮绑定事件即可  用事件委派，这儿是只给一个删除绑定了
  layer.confirm(
    "确认删除？",
    {
      title: "删除",
      btn: ["确认", "取消"], //按钮
    },
    function () {
      //确定的方法
      // console.log(id);
      //赋值给新对象
      let userTypes = goodspj.filter(function (item) {
        // console.log(item.id);
        return item.ID != id;
      });
      // console.log(id);
      // console.log(userTypes);
      goodspj = userTypes;
      addUser("goodspj", goodspj);
      Goodspjappend(goodspj);
      layer.msg("已删除", { icon: 1, time: 500 });
    },
    function () {
      //取消方法
      layer.msg("已取消", { icon: 2, time: 500 });
    }
  );
});

//点击评价跳转委托
$("#tbody-2").on("click", ".layccpj", function () {
  $("#right-content").load("./html/goodspingjia.html");
});
//点击修改的弹出框的点击事件
var index;
$("#tbody-1").on("click", ".laycc", function () {
  let id = parseInt($(this).attr("index-id")) - 1;

  // console.log(id);
  // 给修改按钮绑定事件即可  用事件委派，这儿是只给一个删除绑定了
  index = layer.open({
    type: 1,
    area: ["600px", "360px"],
    shadeClose: true, //点击遮罩关闭
    content: `<div id="tccontent" style="padding:20px;">
    <span class="textsp">ID:</span><input type="text" class="invaluetxt" disabled="disabled" value="${userType[id].ID}"><br />
    <span class="textsp">类型名称:</span><input type="text" class="invaluetxt" value="${userType[id].typeName}"><br />
    <span class="textsp">商品分类模板前缀:</span><input type="text" class="invaluetxt" value="${userType[id].Prefix}"><br />
    <span class="textsp">商品分类模板后缀:</span><input type="text" class="invaluetxt" value="${userType[id].suffix}"><br />
    <span class="textsp">属性列表:</span><input type="text" class="invaluetxt" value="${userType[id].Attributes}"><br />
    <button id="Typexiugai"  class="layui-btn">修改</button>
    <button id="Typequxiao" class="layui-btn">取消</button>
    </div>`,
  });
  //修改点击确定事件
  $("#tccontent").on("click", "#Typexiugai", function () {
    //获取各项属性值
    let ids = parseInt($(".invaluetxt").eq(0).prop("value"));
    let typeName = $(".invaluetxt").eq(1).prop("value");
    let Prefix = $(".invaluetxt").eq(2).prop("value");
    let suffix = $(".invaluetxt").eq(3).prop("value");
    let Attributes = $(".invaluetxt").eq(4).prop("value");
    let typexiugai = {
      ID: ids,
      typeName: typeName,
      Prefix: Prefix,
      suffix: suffix,
      Attributes: Attributes,
    };
    //替换修改后的对象
    userType[id] = typexiugai;
    addUser("userType", userType);
    Typeappend(userType);
    //关闭弹窗
    // $(".layui-layer").css("display", "none");
    layer.close(index);
    // $("#Typexiugai").parent().parent().parent().css("display", "none");
  });

  //点击取消弹出框
  $("#tccontent").on("click", "#Typequxiao", function () {
    //关闭弹窗
    // $(".layui-layer").css("display", "none");
    layer.close(index);
  });
});
//提交返回发货列表
$("#tijiao").click(function () {
  let tname = $("#typename1").val();
  let spqianz = $("#shangping1").val();
  let sphouz = $("#shangping2").val();
  let atbute = "食品类、水产类";
  if (tname == "" || spqianz == "" || sphouz == "" || atbute == "") {
    layer.msg("请填写完整", { icon: 52, time: 500 });
  } else {
    let obj = {
      ID: userType.length + 1,
      typeName: tname,
      Prefix: spqianz,
      suffix: sphouz,
      Attributes: atbute,
    };
    userType.push(obj);
    addUser("userType", userType);
    console.log(userType);
    Typeappend(userType);

    layer.msg("已提交", { icon: 1, time: 500 });
    $("#right-content").load("./html/type_page.html");
  }
});
//提交并继续
$("#tijiaoandre").click(function () {
  let tname = $("#typename1").val();
  let spqianz = $("#shangping1").val();
  let sphouz = $("#shangping2").val();
  let atbute = "食品类、水产类";
  if (tname == "" || spqianz == "" || sphouz == "" || atbute == "") {
    layer.msg("请填写完整", { icon: 52, time: 500 });
  } else {
    let obj = {
      ID: userType.length + 1,
      typeName: tname,
      Prefix: spqianz,
      suffix: sphouz,
      Attributes: atbute,
    };
    layer.msg("已提交", { icon: 1, time: 500 });
    $("#typename1").val("");
    $("#shangping1").val("");
    $("#shangping2").val("");
    userType.push(obj);
    addUser("userType", userType);
    Typeappend(userType);
    console.log(userType);
  }
});
//重置发货信息
$("#chongzhi").click(function () {
  $("#typename1").val("");
  $("#shangping1").val("");
  $("#shangping2").val("");
});
//返回发货列表
$("#return_button").click(function () {
  $("#right-content").load("./html/type_page.html");
});
//返回评价列表
$("#return_button1").click(function () {
  $("#right-content").load("./html/goodspj.html");
});

//回复返回页面
$("#huifu").click(function () {
  layer.msg("已回复", { icon: 1, time: 500 });
  $("#right-content").load("./html/goodspj.html");
});
